{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "lesbian-springer",
   "metadata": {},
   "source": [
    "## Seldon V2 Non Kubernetes Pipeline Version Updates\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fca716ed",
   "metadata": {},
   "source": [
    "### Model Join\n",
    "\n",
    "Join two flows of data from two models as input to a third model."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "fcbc23cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "!seldon model load -f ./models/add10.yaml \n",
    "!seldon model load -f ./models/mul10.yaml "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f8bb2efb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "!seldon model status add10 -w ModelAvailable | jq -M .\n",
    "!seldon model status mul10 -w ModelAvailable | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "cd58ecba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Pipeline\r\n",
      "metadata:\r\n",
      "  name: version-test\r\n",
      "spec:\r\n",
      "  steps:\r\n",
      "    - name: add10\r\n",
      "  output:\r\n",
      "    steps:\r\n",
      "    - add10\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./pipelines/version-test-a.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ce9e163b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline load -f ./pipelines/version-test-a.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7593c77f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"pipelineName\": \"version-test\",\r\n",
      "  \"versions\": [\r\n",
      "    {\r\n",
      "      \"pipeline\": {\r\n",
      "        \"name\": \"version-test\",\r\n",
      "        \"uid\": \"cg5g7ck6dpcs73c4qho0\",\r\n",
      "        \"version\": 1,\r\n",
      "        \"steps\": [\r\n",
      "          {\r\n",
      "            \"name\": \"add10\"\r\n",
      "          }\r\n",
      "        ],\r\n",
      "        \"output\": {\r\n",
      "          \"steps\": [\r\n",
      "            \"add10.outputs\"\r\n",
      "          ]\r\n",
      "        },\r\n",
      "        \"kubernetesMeta\": {}\r\n",
      "      },\r\n",
      "      \"state\": {\r\n",
      "        \"pipelineVersion\": 1,\r\n",
      "        \"status\": \"PipelineReady\",\r\n",
      "        \"reason\": \"created pipeline\",\r\n",
      "        \"lastChangeTimestamp\": \"2023-03-10T10:17:22.883632333Z\",\r\n",
      "        \"modelsReady\": true\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline status version-test -w PipelineReady | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0d03d5fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"outputs\": [\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT\",\r\n",
      "      \"datatype\": \"FP32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"4\"\r\n",
      "      ],\r\n",
      "      \"contents\": {\r\n",
      "        \"fp32Contents\": [\r\n",
      "          11,\r\n",
      "          12,\r\n",
      "          13,\r\n",
      "          14\r\n",
      "        ]\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline infer version-test --inference-mode grpc \\\n",
    " '{\"model_name\":\"outlier\",\"inputs\":[{\"name\":\"INPUT\",\"contents\":{\"fp32_contents\":[1,2,3,4]},\"datatype\":\"FP32\",\"shape\":[4]}]}' | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1d9878b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "apiVersion: mlops.seldon.io/v1alpha1\r\n",
      "kind: Pipeline\r\n",
      "metadata:\r\n",
      "  name: version-test\r\n",
      "spec:\r\n",
      "  steps:\r\n",
      "    - name: mul10\r\n",
      "  output:\r\n",
      "    steps:\r\n",
      "    - mul10\r\n"
     ]
    }
   ],
   "source": [
    "!cat ./pipelines/version-test-b.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a1d77358",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline load -f ./pipelines/version-test-b.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8b92a65c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"pipelineName\": \"version-test\",\r\n",
      "  \"versions\": [\r\n",
      "    {\r\n",
      "      \"pipeline\": {\r\n",
      "        \"name\": \"version-test\",\r\n",
      "        \"uid\": \"cg5g7ek6dpcs73c4qhog\",\r\n",
      "        \"version\": 2,\r\n",
      "        \"steps\": [\r\n",
      "          {\r\n",
      "            \"name\": \"mul10\"\r\n",
      "          }\r\n",
      "        ],\r\n",
      "        \"output\": {\r\n",
      "          \"steps\": [\r\n",
      "            \"mul10.outputs\"\r\n",
      "          ]\r\n",
      "        },\r\n",
      "        \"kubernetesMeta\": {}\r\n",
      "      },\r\n",
      "      \"state\": {\r\n",
      "        \"pipelineVersion\": 2,\r\n",
      "        \"status\": \"PipelineReady\",\r\n",
      "        \"reason\": \"created pipeline\",\r\n",
      "        \"lastChangeTimestamp\": \"2023-03-10T10:17:30.576466739Z\",\r\n",
      "        \"modelsReady\": true\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline status version-test -w PipelineReady | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "73e78ada",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"outputs\": [\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT\",\r\n",
      "      \"datatype\": \"FP32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"4\"\r\n",
      "      ],\r\n",
      "      \"contents\": {\r\n",
      "        \"fp32Contents\": [\r\n",
      "          10,\r\n",
      "          20,\r\n",
      "          30,\r\n",
      "          40\r\n",
      "        ]\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline infer version-test --inference-mode grpc \\\n",
    " '{\"model_name\":\"outlier\",\"inputs\":[{\"name\":\"INPUT\",\"contents\":{\"fp32_contents\":[1,2,3,4]},\"datatype\":\"FP32\",\"shape\":[4]}]}' | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "77c8cb8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline load -f ./pipelines/version-test-a.yaml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "cec42a5b",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"pipelineName\": \"version-test\",\r\n",
      "  \"versions\": [\r\n",
      "    {\r\n",
      "      \"pipeline\": {\r\n",
      "        \"name\": \"version-test\",\r\n",
      "        \"uid\": \"cg5g7g46dpcs73c4qhp0\",\r\n",
      "        \"version\": 3,\r\n",
      "        \"steps\": [\r\n",
      "          {\r\n",
      "            \"name\": \"add10\"\r\n",
      "          }\r\n",
      "        ],\r\n",
      "        \"output\": {\r\n",
      "          \"steps\": [\r\n",
      "            \"add10.outputs\"\r\n",
      "          ]\r\n",
      "        },\r\n",
      "        \"kubernetesMeta\": {}\r\n",
      "      },\r\n",
      "      \"state\": {\r\n",
      "        \"pipelineVersion\": 3,\r\n",
      "        \"status\": \"PipelineReady\",\r\n",
      "        \"reason\": \"created pipeline\",\r\n",
      "        \"lastChangeTimestamp\": \"2023-03-10T10:17:36.568170759Z\",\r\n",
      "        \"modelsReady\": true\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline status version-test -w PipelineReady | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2e54f9b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\r\n",
      "  \"outputs\": [\r\n",
      "    {\r\n",
      "      \"name\": \"OUTPUT\",\r\n",
      "      \"datatype\": \"FP32\",\r\n",
      "      \"shape\": [\r\n",
      "        \"4\"\r\n",
      "      ],\r\n",
      "      \"contents\": {\r\n",
      "        \"fp32Contents\": [\r\n",
      "          11,\r\n",
      "          12,\r\n",
      "          13,\r\n",
      "          14\r\n",
      "        ]\r\n",
      "      }\r\n",
      "    }\r\n",
      "  ]\r\n",
      "}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline infer version-test --inference-mode grpc \\\n",
    " '{\"model_name\":\"outlier\",\"inputs\":[{\"name\":\"INPUT\",\"contents\":{\"fp32_contents\":[1,2,3,4]},\"datatype\":\"FP32\",\"shape\":[4]}]}' | jq -M ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ea766ec6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\r\n"
     ]
    }
   ],
   "source": [
    "!seldon pipeline unload version-test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5e597644",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "!seldon model unload add10\n",
    "!seldon model unload mul10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84d6f01a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
